<?php
    include 'include/admin_function.php';
    ob_implicit_flush();
    $url = "http://acmicpc-live-archive.uva.es/nuevoportal/data/problem.php?p={$_GET['p']}";
    echo "start getting contents\n <br/> \n";
    $content = file_get_contents($url);
    echo "contents got\n <br/> \n";
    
    echo 'getting problem title<br/>';    
    $title = t_get_problem_title($content);
    echo "title: $title \n <br/> \n";
    
    $content = str_replace("<tex2html_verbatim_mark>", "", $content);
    $content = str_replace("<FONT size=4 COLOR=#ff0000>", "", $content);
    $content = str_replace("</FONT>", "", $content);
    $content = str_replace("<SPAN CLASS=\"MATH\">", "", $content);
    $content = str_replace("</SPAN>", "", $content);
    $content = preg_replace("/<A NAME=\"SECTION(.*?)\">/", "", $content);
    $content = str_replace("</A>", "", $content);
    $content = str_replace("<I>", "<em>", $content);
    $content = str_replace("</I>", "</em>", $content);
    $content = str_replace("<BR>", "<br/>", $content);
//    $content = preg_replace("/<(\/?)(PRE|SUB|LI|OL|UL|SUP)>/e", '"<\\1".strtolower("\\2").">"', $content);
    $content = str_replace("<SPAN  CLASS=\"textit\">", "", $content);
    $content = str_replace("<SPAN  CLASS=\"textbf\">", "", $content);
    $content = preg_replace("/<hr><ADDRESS>([\s\S]*?)<\/html>/", "", $content);
    $content = preg_replace("/<table cellpadding=3 cellspacing=0 border=1 align=center>(.*?)<\/table>/", "", $content);
    $content = preg_replace('/<IMG([^>]+)ALT="\\$ \\\\(le|ge)\\$">/', "&\\2;", $content);
    $content = preg_replace('/<html>([\s\S]*?)<\/p>/', "", $content);
    $content = preg_replace("/<!--(.|\s)*?-->/", "", $content);
    $content = preg_replace("/<TT>((.|\s)*?)<\/TT>/", "\\1", $content);
    
    $content = preg_replace("/<IMG([^>]+)SRC=\"(.+?)\"([^>]+)>/", "<img src=\"http://acm.uva.es/archive/nuevoportal/data/\\2\">", $content);
    
    $out = preg_split("/(<H2>|<\/H2>)/", $content, -1);
//    print_r($out);
    
    $problem['title'] = $title;
    
    
    for ($i = 0; $i < count($out); $i++)
        print htmlspecialchars($out[$i]). "\n<hr>\n";
    
//    preg_match("<(\S*?)[^>]*>.*?</\1>|<.*? />");
    echo $content;
    
function t_get_problem_title($str) {
    $pattern = "/.+<title>\\d+?\\s+?-\\s+?(.+?)<\/title>.+/is";
    $replace = '\1';
    return preg_replace($pattern, $replace, $str);
}
?>
